<?php
declare (strict_types=1);

namespace app\business\controller;

use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\App;
use think\response\Json;

class VideoHit extends Base
{
    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\UserVideoHit';

    //关联模型
    protected $hasOne = ['video', 'user'];

    protected $append = ['video_link_url'];

    //额外查询条件
    protected $extraSearch = ['business_id'];

    public function index()
    {
        $extra_map = ['business_id'=>$this->admin['business_id']];
        return $this->setTable($extra_map);
    }

    //自动构建table,按需修改
    public function setTable($extra_map=[])
    {
        //构建table
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'id', 'title' => 'ID', 'sort' => true, 'width' => 80])
            ->addRow(['field' => 'icon', 'title' => '视频', 'width' => 90, 'templet' => '#iconTpl'])
            ->addRow(['field' => 'introduce', 'title' => '视频介绍', 'width' => 1000, 'templet' => '<div>{{d.video?d.video.introduce:""}}</div>'])
            ->addRow(['field' => 'user', 'title' => '会员', 'templet' => '<div>{{d.user?d.user.mobile:""}}</div>'])
            ->addRow(['field' => 'create_time', 'title' => '点赞时间'])
            ->clearTable();

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setSearch(), false)
            ->addExtraMap($extra_map)
            ->addExtraHtml(['VideoComment'])
            ->fetch();
    }

    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['video_id', '视频标题', 'input', '', '\app\common\model\BusinessVideo', 'title,id'])
            ->setSearch(['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id']);
    }
}